<?php

namespace common\models\user;

use Yii;
use common\helpers\Url;

/**
 * This is the model class for table "{{%user_authent}}".
 *
 * @property int $id
 * @property int $user_id
 * @property string $realname
 * @property string|null $ticketno 人像照
 * @property int $type
 * @property string $lntsno
 * @property int $audit_at
 * @property string|null $mark 审核备注
 * @property int $status
 * @property int $created_at
 * @property int $updated_at
 */
class UserAuthent extends \common\db\ActiveRecord {

    //
    const STATUS_DEFAULT = 0;
    const STATUS_AUDITING = 1;
    const STATUS_REFUSE = -1;
    const STATUS_CANCEL = -2;
    const STATUS_ACTIVE = 10;

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return '{{%user_authent}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['user_id', 'type', 'audit_at', 'status', 'created_at', 'updated_at'], 'integer'],
            [['realname'], 'required'],
            [['realname'], 'string', 'max' => 50],
            [['mark'], 'string'],
            [['ticketno', 'lntsno'], 'string', 'max' => 255],
            [['user_id'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'user_id' => '用户ID',
            'realname' => '真实姓名',
            'ticketno' => '年票号',
            'type' => '认证类型',
            'lntsno' => '会员号',
            'audit_at' => '审核时间',
            'mark' => '审核失败原因',
            'status' => '认证状态',
            'created_at' => '认证时间',
            'updated_at' => '最后更新时间',
        ];
    }

    public function checkUpdatePermission() {
        if ($this->isNewRecord || $this->status === static::STATUS_REFUSE) {
            return true;
        } elseif ($this->status === static::STATUS_ACTIVE) {
            return $this->dumpError('您已通过了实名认证，无需重新认证');
        } elseif ($this->status === static::STATUS_DEFAULT) {
            return $this->dumpError('您的认证信息正在审核中，不能修改');
        } else {
            return $this->dumpError('认证操作失败未知错误，可能您的账户信息异常');
        }
    }

    public function getIsActive() {
        return $this->status == static::STATUS_ACTIVE;
    }

    public function setApply() {
        $this->status = static::STATUS_DEFAULT;
    }

    /**
     * 获取审核状态
     * @inheritdoc
     */
    public function getAuditStatus() {
        if ($this->status == static::STATUS_REFUSE) {
            return [
                'icon' => Url::attach('/assets/icons/authent/audit_refuse.png'),
                'text' => '审核未通过',
            ];
        } elseif ($this->status == static::STATUS_DEFAULT) {
            return [
                'icon' => Url::attach('/assets/icons/authent/auditing.png'),
                'text' => '审核中',
            ];
        } elseif ($this->status == static::STATUS_ACTIVE) {
            return [
                'icon' => Url::attach('/assets/icons/authent/allowed.png'),
                'text' => '认证通过',
            ];
        } elseif ($this->status == static::STATUS_AUDITING) {
            return [
                'icon' => Url::attach('/assets/icons/authent/auditing.png'),
                'text' => '审核中',
            ];
        }
        return [
            'icon' => Url::attach('/assets/icons/authent/abnormal.png'),
            'text' => '信息异常',
        ];
    }

    public function getText() {
        if ($this->status == static::STATUS_DEFAULT) {
            return '认证审核中';
        } elseif ($this->status == static::STATUS_ACTIVE) {
            return '已认证';
        }
        return '审核失败';
    }

    public function getLink() {
        return Url::h5('authent');
    }

    public function getNormalView() {
        return [
            'name' => $this->realname,
            'lntsno' => $this->lntsno,
        ];
    }

    public function fields() {
        return [
            'status',
            'text',
            'link',
        ];
    }

    public static function findByUserId($uid) {
        return static::find()->active()->andWhere([
            'user_id' => $uid
        ])->one();
    }

}
